/** 
 * Project Name:kaka 
 * File Name:BusinessCardAction.java 
 * Package Name:com.luoran.kaka.api.wechat 
 * Date:2018年12月13日下午2:48:18 
 * Copyright (c) 2018, Luoran, Inc. All Rights Reserved.
 * 
 */
package com.luoran.kaka.api.wechat;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.luoran.kaka.entity.BasicInfo;
import com.luoran.kaka.entity.CompanyInfo;
import com.luoran.kaka.service.IBasicInfoService;
import com.luoran.kaka.service.IBusinessCardHolderService;
import com.luoran.kaka.service.ICompanyInfoService;

/** 
 * @author ghl 
 * @since JDK 1.8
 * ClassName: BusinessCardAction
 * date: 2018年12月13日 下午2:48:18
 *	名片业务action
 */
@Controller
@RequestMapping("/api/my")
public class BusinessCardAction {

	private static final Logger log = LoggerFactory.getLogger(BusinessCardAction.class);

	@Autowired
	private IBasicInfoService basicInfoService;

	@Autowired
	private ICompanyInfoService companyInfoService;

	@Autowired
	private IBusinessCardHolderService businessCardHolderService;

	/** 
	 * @author ghl  by 2018年12月14日下午4:50:13
	 * @param openId
	 * @return 
	 * @since JDK 1.8
	 * getAllMyPersonalCard:获取当前用户所有的个人名片
	 */
	@RequestMapping("/personal/card/{openId}")
	@ResponseBody
	public JSONObject getAllMyPersonalCard(@PathVariable("openId") String openId) {
		log.info("获取当前用户所有的个人名片，参数openId为：{{}}", openId);
		JSONObject obj = new JSONObject();
		List<BasicInfo> list = basicInfoService.getAllMyPersonalCardByOpenId(openId);
		if (null != list && list.size() > 0) {
			obj.put("flag", true);
			obj.put("list", list);
		} else {
			obj.put("flag", false);
			obj.put("msg", "没有个人名片，点击新建");
		}
		return obj;
	}


	/** 
	 * @author ghl  by 2018年12月14日下午4:55:08
	 * @param openId
	 * @return 
	 * @since JDK 1.8
	 * getAllMyCompanyCard:获取当前用户所有的企业名片
	 */
	@RequestMapping("/company/card/{openId}")
	@ResponseBody
	public JSONObject getAllMyCompanyCard(@PathVariable("openId") String openId) {
		log.info("获取当前用户所有的企业名片，参数openId为：{{}}", openId);
		JSONObject obj = new JSONObject();
		List<CompanyInfo> list = companyInfoService.getAllMyCompanyCardByOpenId(openId);
		if (null != list && list.size() > 0) {
			obj.put("flag", true);
			obj.put("list", list);
		} else {
			obj.put("flag", false);
			obj.put("msg", "没有企业名片，点击新建");
		}
		return obj;
	}


	/** 
	 * @param openId
	 * @param type
	 * @return 
	 * @since JDK 1.8
	 * @author ghl  by 2018年12月19日下午2:44:25
	 * getMyCardHolder://当选项卡在选中个人时,查询名片夹中的个人名片.此时type=1，当选项卡在选中企业时,查询名片夹中的企业名片.此时type=2
	 */ 
	@RequestMapping("/card/holder/{type}/{openId}")
	@ResponseBody
	public JSONObject getMyCardHolder(@PathVariable("openId") String openId, @PathVariable("type") Integer type) {
		log.info("获取当前用户的名片夹，参数openId为：{{}}，参数type为：{{}}", openId, type);
		JSONObject obj = new JSONObject();
		List<Object> list = null;
		try {
			list = businessCardHolderService.getCardsFromHolder(openId, type);
			if (list != null && list.size() > 0) {
				obj.put("flag", true);
				obj.put("list", list);
			} else {
				obj.put("flag", false);
				obj.put("msg", "暂未收藏名片~！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			obj.put("flag", false);
			obj.put("msg", "暂未收藏名片~！");
		}
		return obj;


	}
}
